package com.jp.knowledge.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.comm.InfoDetailData;
import com.jp.knowledge.R;
import com.jp.knowledge.activity.InfoTopicDetailActivity;
import com.jp.knowledge.command.Command;
import com.jp.knowledge.dialog.CommDialog;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.model.TopicListInfo;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.base.RecycleVHolder;
import com.jp.knowledge.util.ImageLoadUtil;
import com.jp.knowledge.util.ResultSubscriber;
import com.jp.knowledge.util.TimeUtil;

import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/1/14.
 */

public class InfoTopicAdapter extends RecycleVAdapter<TopicListInfo> implements RecycleVAdapter.OnItemClickListener, View.OnClickListener, ResultSubscriber.OnResultListener {

    private static final int ITEM_PRAISE_CODE = 0x01;
    private static final int DELETE_CODE = 0x02;
    private InfoDetailData infoDetailData;
    private String uuid;
    private CommDialog deleteDialog;

    public InfoTopicAdapter(Context context, @NonNull List<TopicListInfo> list, String uuid) {
        super(context, list);
        infoDetailData = InfoDetailData.getInfoDetail();
        this.uuid = uuid;
        deleteDialog = new CommDialog(mContext);
        deleteDialog.setContent("是否删除这条评论？");
        deleteDialog.setRight("删除");
        deleteDialog.setTag(0);
        deleteDialog.setRightListener(this);
        addItemClickListener(this);
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.item_info_topic;
    }

    @Override
    public void onBind(RecycleVHolder holder, int position) {
        TopicListInfo topicListInfo = getData(position);
        ImageLoadUtil.loadCircle(mContext,
                topicListInfo.getPortrait() + "?imageView2/2/w/80/interlace/1",
                holder.getImageView(R.id.item_topic_info_head));
        holder.setTextView(R.id.item_topic_info_name, topicListInfo.getUserName());
        holder.setTextView(R.id.item_topic_info_comment_text, topicListInfo.getReplyNum()+"");
        holder.setTextView(R.id.item_topic_info_praise_text, topicListInfo.getPraiseNum()+"");
        holder.get(R.id.item_topic_info_praise_img).setTag(position);
        holder.get(R.id.item_topic_info_praise_img).setOnClickListener(this);
        if(topicListInfo.getIsPraise() == 1){
            holder.getTextView(R.id.item_topic_info_praise_text)
                    .setTextColor(mContext.getResources().getColor(R.color.font_orign));
            holder.getImageView(R.id.item_topic_info_praise_img).setImageResource(R.mipmap.give_the_thumbs_up_lan);
        } else{
            holder.getTextView(R.id.item_topic_info_praise_text)
                    .setTextColor(mContext.getResources().getColor(R.color.font_gray_think));
            holder.getImageView(R.id.item_topic_info_praise_img).setImageResource(R.mipmap.give_the_thumbs_up);
        }
        holder.setTextView(R.id.item_topic_info_time, TimeUtil.friendly_time(topicListInfo.getTime()));

        TextView deleteBtn = holder.getTextView(R.id.item_topic_info_delete);
        deleteBtn.setTag(position);
        if(topicListInfo.getUid() != null && topicListInfo.getUid().equals(uuid)){
            deleteBtn.setVisibility(View.VISIBLE);
            deleteBtn.setOnClickListener(this);
        } else{
            deleteBtn.setVisibility(View.GONE);
        }
        holder.setTextView(R.id.item_topic_info_content, topicListInfo.getContent());
        if(topicListInfo.getReplyNum() > 0){
            holder.get(R.id.item_topic_info_reply_view).setVisibility(View.VISIBLE);
            TopicListInfo.ReplyDataBean replyDataBean = topicListInfo.getReplyData();
            holder.setTextView(R.id.item_topic_info_reply_name, replyDataBean.getUserName() + "：");
            holder.setTextView(R.id.item_topic_info_reply_content, replyDataBean.getContent());
            if(topicListInfo.getReplyNum() == 1) {
                holder.get(R.id.item_topic_info_reply_more).setVisibility(View.GONE);
            } else{
                holder.get(R.id.item_topic_info_reply_more).setVisibility(View.VISIBLE);
                holder.setTextView(R.id.item_topic_info_reply_more,
                        "更多" + topicListInfo.getReplyNum() + "条回复");
            }
        } else{
            holder.get(R.id.item_topic_info_reply_view).setVisibility(View.GONE);
        }
    }

    @Override
    public void itemSelect(int position) {
        Intent intent = new Intent(mContext, InfoTopicDetailActivity.class);
        intent.putExtra(InfoTopicDetailActivity.COMMENT_POSITION, position);
        mContext.startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.item_topic_info_delete){
            int position = (int) v.getTag();
            deleteDialog.setTag(position);
            deleteDialog.show();
        } else if(v.getId() == R.id.item_topic_info_praise_img) {
            int position = (int) v.getTag();
            if (mList.get(position).getIsPraise() == 1) {
                ToasUtil.toast(mContext, "请勿重复点赞");
                return;
            }
            TopicListInfo topicListInfo = getData(position);
            JsonObject param = new JsonObject();
            param.addProperty("token", HttpHelper.token);
            param.addProperty("topicId", infoDetailData.getTopicInfo().getTopicId());
            param.addProperty("infoId", topicListInfo.getInfoId());
            param.addProperty("commentId", topicListInfo.getCommentId());
            param.addProperty("type", infoDetailData.getLinkType());
            HttpHelper.getInstance(mContext).topicPraise(param, position * 10 + ITEM_PRAISE_CODE, this);
        } else if(v.getId() == R.id.right_btn){
            int position = (int) deleteDialog.getTag();
            JsonObject param = new JsonObject();
            param.addProperty("commentId", getData(position).getCommentId());
            HttpHelper.getInstance(mContext).commentDelete(param, position * 10 + DELETE_CODE, this);
            removeData(position);
        }
    }

    @Override
    public void onStart(int requestType) {}
    @Override
    public void onCompleted(int requestType) {}
    @Override
    public void onError(int requestType) {
        onCompleted(requestType);
    }
    @Override
    public void onNext(IModel data, int requestType) {
        if(data.getErrcode() != 0){
            ToasUtil.toast(mContext, data.getMessage());
            onError(requestType);
        } else if(requestType%10 == ITEM_PRAISE_CODE) {
            int position = requestType / 10;
            mList.get(position).setIsPraise(1);
            mList.get(position).setPraiseNum(mList.get(position).getPraiseNum() + 1);
            notifyDataSetChanged();
        } else if(requestType%10 == DELETE_CODE){

        }
    }
}
